package com.patui.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
public class ImageAndText extends FrameLayout{
	private ImageView 		mImgView;
	private TextView 		mTxtView;
	private String 			mStrOfTxt;
	private Bitmap 			mBmOfImg=null;
	private int 			mIntResId=-1;
	private int             mIntResId_Press;
	
    public ImageAndText(Context context) {
		super(context);
		initLayout(context);
		// TODO Auto-generated constructor stub
	}
	
	public ImageAndText(Context context, Bitmap mImg, String mTxtView) {
		super(context);
		this.mBmOfImg = mImg;
		this.mStrOfTxt = mTxtView;
		initLayout(context);
	}
	
	public ImageAndText(Context context, int resId, String mTxtView) {
		super(context);
		this.mIntResId = resId;
		this.mStrOfTxt = mTxtView;
		initLayout(context);
	}
	
	public ImageAndText(Context context, int resId,int resId_Press,String mTxtView) {
		super(context);
		this.mIntResId = resId;
		this.mIntResId_Press=resId_Press;
		this.mStrOfTxt = mTxtView;
		initLayout(context);
	}
	
	private void initLayout(Context context) {
		// TODO Auto-generated method stub
		LayoutParams mFrameParams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		mFrameParams.gravity=Gravity.CENTER;
		mImgView=new ImageView(context);
		mImgView.setScaleType(ScaleType.CENTER_INSIDE);
		addView(mImgView, mFrameParams);
		
		mFrameParams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		mFrameParams.gravity=Gravity.CENTER;
		mTxtView=new TextView(context);
		mTxtView.setTextColor(0xffffffff);
		mTxtView.setText("");
		addView(mTxtView,mFrameParams);
		if(mIntResId!=-1)
		{
			initDataByResId(mIntResId,mStrOfTxt);
		}
		else if(mBmOfImg!=null)
		{
			initDataByBm(mBmOfImg,mStrOfTxt);
		}
	}
	
	

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		int act = event.getAction();
		if(act ==  MotionEvent.ACTION_DOWN)
		{	
			mImgView.setBackgroundResource(mIntResId_Press);
		}
		if(act ==  MotionEvent.ACTION_UP)
		{	
			mImgView.setBackgroundResource(mIntResId);
		}
		if(act == MotionEvent.ACTION_OUTSIDE)
		{
			mImgView.setBackgroundResource(mIntResId);
		}
		return super.onTouchEvent(event);
	}

	public void initDataByBm(Bitmap bm,String txt) {
		// TODO Auto-generated method stub
		mImgView.setImageBitmap(bm);
		mTxtView.setText(txt);
		
	}
	
	public void initDataByResId(int resId,String txt) {
		// TODO Auto-generated method stub
		mImgView.setImageResource(resId);
		mTxtView.setText(txt);	
	}
	
	public void setImgeViewWidth(int newWidth)
	{
		LayoutParams params = (LayoutParams)mImgView.getLayoutParams();
		params.width=newWidth;
		mImgView.setLayoutParams(params);
	}
	
	public void setStrOfText(int selectNo)
	{
        mTxtView.setText(String.valueOf(selectNo));
	}
	
	public void setTextSize() {
		// TODO Auto-generated method stub
		LayoutParams params = (LayoutParams)mTxtView.getLayoutParams();
		params.bottomMargin=5;
		mTxtView.setTextSize(22.0f);
	}
	
	
	public void setImageByResId(int resId) {
		// TODO Auto-generated method stub
		mImgView.setImageResource(resId);
	}
	
	public void setTextColor(int color) {
		// TODO Auto-generated method stub
		mTxtView.setTextColor(color);
	}


}
